package yns.springboot.common.sample.controller;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import yns.springboot.common.sample.dto.DataInfoDto;
import yns.springboot.common.sample.entity.DataInfoEntity;
import yns.springboot.common.sample.page.PaginationInfo;
import yns.springboot.common.sample.service.DataInfoService;
import yns.springboot.common.sample.utility.time.TimeUtility;


@RestController
@CrossOrigin
@RequestMapping("/search")
public class SearchController {

	private final Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private DataInfoService dataInfoService;

	@GetMapping()
	public String search() {
		logger.info("search");

		UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		logger.info(userDetails.getUsername());

		return "test";
	}

	@GetMapping("/getDataInfo")
	public String getDataInfo() {
		logger.info("getDataInfo");

		List<DataInfoDto> dataInfoEntities = dataInfoService.getDataInfo();
		String result = JSON.toJSON(dataInfoEntities).toString();
		
		logger.info("getDataInfo : " + result);
		
		return result;
	}

	@PostMapping("/addDataInfo")
	public String addDataInfo(@RequestBody String jsonData) {
		logger.info("addDataInfo");
		logger.info("addDataInfo : " + jsonData);
		
//		DataInfoDto dataInfoDto = new DataInfoDto();
//		dataInfoDto.setName("Name : " + TimeUtility.getCurrentTime());
//		dataInfoDto.setDataTime(TimeUtility.getCurrentTime());
//		dataInfoDto.setDescription(TimeUtility.getCurrentTime());
		
		DataInfoDto dataInfoDto2 = JSON.parseObject(jsonData,DataInfoDto.class);
		logger.info("addDataInfo dataInfoDto2 : " + dataInfoDto2);
		

		int result = dataInfoService.addDataInfo(dataInfoDto2);
		
		JSONObject resultJsonObject = new JSONObject();
		resultJsonObject.put("result", result);
		
		logger.info("addDataInfo : " + resultJsonObject.toJSONString());
		
		return resultJsonObject.toJSONString();
	}

	@GetMapping("/getDataInfoByID/{id}")
	public String getDataInfoByID(@PathVariable(name="id") int id) {
		logger.info("getDataInfoByID");
		
		DataInfoDto dataInfoDto = dataInfoService.getDataInfoByID(id);
		String result = JSON.toJSON(dataInfoDto).toString();
		
		logger.info("getDataInfoByID : " + result);
		
		return result;
	}

	@PutMapping("/updateDataInfo")
	public String updateDataInfo(@RequestBody String jsonData) {
		logger.info("updateDataInfo");
		
//		JSONObject updateInfo = JSON.parseObject(jsonData);
		
//		DataInfoDto dataInfoDto = new DataInfoDto();
//		dataInfoDto.setId(updateInfo.getIntValue("id"));
//		dataInfoDto.setDescription(updateInfo.getString("description"));
		
		DataInfoDto dataInfoDto2 = JSON.parseObject(jsonData,DataInfoDto.class);
		logger.info("updateDataInfo dataInfoDto2 : " + dataInfoDto2);
		
		int result = dataInfoService.updateDataInfo(dataInfoDto2);
		
		JSONObject resultJsonObject = new JSONObject();
		resultJsonObject.put("result", result);
		
		logger.info("updateDataInfo : " + resultJsonObject.toJSONString());

		return resultJsonObject.toJSONString();
	}
	
	
	@PostMapping("/page")
	public String page(@RequestBody String jsonData)  {
		logger.info("page");
		
		PaginationInfo<DataInfoEntity> paginationInfo = JSON.parseObject(jsonData,PaginationInfo.class);
		
		logger.info("page paginationInfo : " + paginationInfo);
		
		List<DataInfoDto> list = dataInfoService.getPage(paginationInfo.getCurrentPage(), paginationInfo.getPageSize());
		int totalCount = dataInfoService.getTotalCount();
		
		JSONObject resultJsonObject = new JSONObject();
		resultJsonObject.put("list", list);
		resultJsonObject.put("totalCount", totalCount);
		
		logger.info("page : " + resultJsonObject.toJSONString());

		return resultJsonObject.toJSONString();
	}

}
